﻿using Abp.Domain.Repositories;
using MyCompanyName.AbpZeroTemplate.Prm.Project;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class DocLogManager : AbpZeroTemplateDomainServiceBase
    {
        IRepository<DocLog, int> docLogRepository;
        public DocLogManager(
                 IRepository<DocLog, int> _docLogRepository)
        {
            docLogRepository = _docLogRepository;
        }


        /// <summary>
        /// 上传文档
        /// </summary>
        /// <param name="log"></param>
        public void UploadDocLog(ProjectDocmeut doc,string projectNo)
        {

            DocLog log = new DocLog()
            {
                BelongTo = projectNo,
                OptType = OptType.上传,
                FileGuid = doc.FullUrl,
                CreateBy = doc.CreationUser,
                CreateOn = DateTime.Now,
                DocType = doc.Extension,
                FileName = doc.Name,
                Remark = doc.Note
            };
            docLogRepository.Insert(log);

        }

        /// <summary>
        /// 定版文档
        /// </summary>
        /// <param name="fileGuid"></param>
        /// <param name="fileName"></param>
        /// <param name="docType"></param>
        /// <param name="CreateBy"></param>
        public void ConfirmDocLog(string fileGuid, string projectNo,string createBy)
        {
            var uplog = docLogRepository.GetAll().Where(x => x.FileGuid == fileGuid && x.OptType == OptType.上传).FirstOrDefault();
            if (uplog != null)
            {
                DocLog log = new DocLog()
                {
                    BelongTo = projectNo,
                    OptType = OptType.定版,
                    FileGuid = fileGuid,
                    CreateBy = createBy,
                    CreateOn = DateTime.Now,
                    DocType = uplog.DocType,
                    FileName = uplog.FileName,
                    Remark = string.Empty
                };
                docLogRepository.Insert(log);
            }
        }

        /// <summary>
        /// 下载文档
        /// </summary>
        /// <param name="fileGuid"></param>
        /// <param name="CreateBy"></param>
        public void DownLoadDocLog(string fileGuid, string CreateBy)
        {
            var uplog = docLogRepository.GetAll().Where(x => x.FileGuid == fileGuid && x.OptType == OptType.上传).FirstOrDefault();
            var belongTos = docLogRepository.GetAll().Where(x => x.FileGuid == fileGuid && x.BelongTo != "").Select(x => x.BelongTo).Distinct().ToList();

            if (uplog != null)
            {
                DocLog log = new DocLog()
               {
                   BelongTo = string.Join(";", belongTos),
                   OptType = OptType.下载,
                   FileGuid = fileGuid,
                   CreateBy = CreateBy,
                   CreateOn = DateTime.Now,
                   DocType = uplog.DocType,
                   FileName = uplog.FileName,
                   Remark = string.Empty
               };
                docLogRepository.Insert(log);

            }
        }

        /// <summary>
        /// 查看文档
        /// </summary>
        /// <param name="fileGuid"></param>
        /// <param name="CreateBy"></param>
        public void ViewDocLog(string fileGuid, string CreateBy)
        {
            var uplog = docLogRepository.GetAll().Where(x => x.FileGuid == fileGuid && x.OptType == OptType.上传).FirstOrDefault();
            if (uplog != null)
            {

                DocLog log = new DocLog()
               {
                   BelongTo = string.Empty,
                   OptType = OptType.查看,
                   FileGuid = fileGuid,
                   CreateBy = CreateBy,
                   CreateOn = DateTime.Now,
                   DocType = uplog.DocType,
                   FileName = uplog.FileName,
                   Remark = uplog.Remark
               };
                docLogRepository.Insert(log);

            }
        }


        public void DeleteDocLog(string fileGuid, string belongTo, string CreateBy,string remark)
        {
            var uplog = docLogRepository.GetAll().Where(x => x.FileGuid == fileGuid && x.OptType == OptType.上传).FirstOrDefault();
            if (uplog != null)
            {

                DocLog log = new DocLog()
               {
                   BelongTo = belongTo,
                   OptType = OptType.删除,
                   FileGuid = fileGuid,
                   CreateBy = CreateBy,
                   CreateOn = DateTime.Now,
                   DocType = uplog.DocType,
                   FileName = uplog.FileName,
                   Remark = remark
               };
                docLogRepository.Insert(log);
            }
        }

        public void ShareDocLog(string fileGuid, string belongTo, string CreateBy)
        {
            var uplog = docLogRepository.GetAll().Where(x => x.FileGuid == fileGuid && x.OptType == OptType.上传).FirstOrDefault();
            if (uplog != null)
            {

                DocLog log = new DocLog()
                {
                    BelongTo = belongTo,
                    OptType = OptType.分享,
                    FileGuid = fileGuid,
                    CreateBy = CreateBy,
                    CreateOn = DateTime.Now,
                    DocType = uplog.DocType,
                    FileName = uplog.FileName,
                    Remark = uplog.Remark
                };
                docLogRepository.Insert(log);
            }
        }

     

        /// <summary>
        /// 下载汇总数据
        /// </summary>
        /// <param name="Remrak"></param>
        /// <param name="CreateBy"></param>
        public void DownLoadTmpLog(string fileName, string Remrak, string CreateBy)
        {
            DocLog log = new DocLog()
           {
               BelongTo = fileName,
               OptType = OptType.下载,
               FileGuid = string.Empty,
               CreateBy = CreateBy,
               CreateOn = DateTime.Now,
               DocType = string.Empty,
               FileName = fileName,
               Remark = Remrak,
               Id = 0
           };
            docLogRepository.Insert(log);
        }


    }
}
